/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.om.dto.DeviceQueryResp;
import com.excesys.exsecs.om.dto.DeviceResp;
import com.excesys.exsecs.om.entity.OmDeviceModelClassification;
import com.excesys.exsecs.om.entity.OmInspectionArea;
import com.excesys.exsecs.om.entity.OmInspectionItems;
import com.excesys.exsecs.om.mapper.OmDeviceModelClassificationMapper;
import com.excesys.exsecs.om.mapper.OmEvenAndDeviceMapper;
import com.excesys.exsecs.om.mapper.OmInspectionAreaMapper;
import com.excesys.exsecs.om.mapper.OmInspectionItemsMapper;
import com.excesys.exsecs.om.service.OmDeviceModelClassificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @date 2023-05-19
 */
@RequiredArgsConstructor
@Service
public class OmDeviceModelClassificationServiceImpl
		extends ServiceImpl<OmDeviceModelClassificationMapper, OmDeviceModelClassification>
		implements OmDeviceModelClassificationService {

	private final OmEvenAndDeviceMapper omEvenAndDeviceMapper;

	private final OmInspectionItemsMapper omInspectionItemsMapper;

	private final OmDeviceModelClassificationMapper omDeviceModelClassificationMapper;

	private final OmInspectionAreaMapper omInspectionAreaMapper;

	@Override
	public List<DeviceQueryResp> getIdList(String id) {
		List<DeviceQueryResp> deviceQueryRespList = new ArrayList<>();
		List<OmInspectionArea> list = omInspectionAreaMapper.getList(id);
		DeviceQueryResp deviceQueryResp = OmDeviceModelClassificationServiceImpl
				.split(list.get(0).getAssociatedDevice());
		if (StringUtils.isEmpty(list.get(0).getAssociatedDevice())) {
			List<DeviceResp> deviceList = new ArrayList<>();
			deviceQueryResp.setDeviceList(deviceList);
		}
		deviceQueryRespList.add(deviceQueryResp);
		return deviceQueryRespList;
	}

	@Override
	public void deleteByCode(String regionFacilityCode, Integer itemsId) {
		try {
			omDeviceModelClassificationMapper.deleteByCode(regionFacilityCode, itemsId);
		}
		catch (Exception e) {
			log.error("数据库未查询到该数据");
			e.printStackTrace();
		}

	}

	@Override
	public List<OmDeviceModelClassification> getList(Integer id) {
		List<OmDeviceModelClassification> om = new ArrayList<>();
		try {
			if (omDeviceModelClassificationMapper.getModelList(id).size() > 0) {
				om = omDeviceModelClassificationMapper.getModelList(id);
			}
		}
		catch (Exception e) {
			log.error("设备分类无数据");
			return om;
		}
		return om;

	}

	@Override
	public void deleteAllByCode(String regionFacilityCode) {
		omDeviceModelClassificationMapper.deleteAllByCode(regionFacilityCode);
	}

	@Override
	public List<OmDeviceModelClassification> getchlidList(String parentId, String id) {
		List<OmDeviceModelClassification> om = omDeviceModelClassificationMapper.getchlidList(parentId);
		return om;
	}

	@Override
	public List<OmDeviceModelClassification> getSize(String parentId, String id, String code) {
		List<OmDeviceModelClassification> om = omDeviceModelClassificationMapper.getSize(parentId, id, code);
		return om;
	}

	@Override
	public List<OmDeviceModelClassification> getchlList(String parentId) {
		List<OmDeviceModelClassification> om = omDeviceModelClassificationMapper.getchlList(parentId);
		return om;
	}

	@Override
	public void deleteAll(String id) {
		omEvenAndDeviceMapper.deleteAll(id);
	}

	@Override
	public List<OmInspectionItems> getItemsList(String id) {
		List<OmInspectionItems> list = new ArrayList<>();
		List<OmDeviceModelClassification> lists = omDeviceModelClassificationMapper.getItemsList(id);
		for (OmDeviceModelClassification om : lists) {
			OmInspectionItems items = omInspectionItemsMapper.selectById(om.getItemsId());
			list.add(items);
		}
		return list;
	}

	public static DeviceQueryResp split(String code) {
		DeviceQueryResp deviceQueryResp = new DeviceQueryResp();
		List<DeviceResp> list = new ArrayList<>();
		String[] strings = code.split(";");
		for (int i = 0; i < strings.length; i++) {
			DeviceResp deviceResp = new DeviceResp();
			System.out.println("输出大分割：" + strings[i]);
			String[] s = strings[i].split(":");
			for (int j = 0; j < s.length; j++) {
				System.out.println("输出小分割：" + s[j]);
				if (j % 2 == 0) {
					deviceResp.setRegionName(s[j]);
				}
				else {
					deviceResp.setRegionCode(s[j]);
				}
			}
			list.add(deviceResp);
		}
		deviceQueryResp.setDeviceList(list);
		return deviceQueryResp;
	}

	@Override
	public void deleteBychlCode(String regionFacilityCode, Integer id) {
		omDeviceModelClassificationMapper.deleteBychlCode(regionFacilityCode, id);
	}

	@Override
	public OmDeviceModelClassification getListByItemId(String id) {
		return omDeviceModelClassificationMapper.getListByItemId(id);
	}

	@Override
	public List<OmDeviceModelClassification> getchlByItemsList(String itemsId, String code) {
		return omDeviceModelClassificationMapper.getchlByItemsList(itemsId, code);
	}

}
